<?php //$Rev:: 23         $ // DO NOT REMOVE

error_reporting(E_ALL);

/**
 * Parser class (Parse Instant Messages)
 * 
 * @author Andrew Heebner <andrew@evilwalr.us>
 * @author Eric Gach <eric@php-oop.net>
 * @version $Revision: 23 $
 * @require PHP 5.0
 */
class Parser
{
    
    public $core;

    /**
     * __construct: Parser constructor
     * 
     * @param object $object Object to store for parent referencing
     * @access public
     * @return void 
     *
     */
     public function __construct(ATAC $object)
     {
         $this->core = $object;
     }
     
     /**
      * __destruct: Parser destructor
      * 
      * @access public
      * @return void
      */
      public function __destruct()
      {
          unset($this->core);
      }
      
      /**
       * __call: Parser invalid method parser
       * 
       * @param string $method Invalid method called
       * @access private
       * @return void
       * @throws ATACException
       *
       */
       public function __call($method, $args)
       {
           throw new ATACException('Invalid method called => (' . __CLASS__ . '::' . $method . ')');
       }
       
       /**
        * aim_parse_im: Parse IM for alerts and text and returns function
        *
        * @param array $data Data feed to parse
        * @access private
        * @return boolean
        * @throws ATACException
        * @proto boolean aim_parse_im(array im_data)
        */
        public function aim_parse_im($data)
        {
            if (!empty($data) && is_array($data)) {
                @list($alert, $text) = explode(' ', $data['message_text'], 2);
                $alert = trim($alert);
                $text = trim($text);
                if (@$alert{0} == $this->core->user['msgalert']) {
                    $alert = strtolower(substr($alert, 1));
                    $data['message_alert'] = $alert;
                    $data['message_text'] = (!empty($text)) ? $text : null;
                    if ($this->core->user['debug'] === true) print_r($data);
                    if (array_key_exists($alert, $this->core->handlers)) {
                        $newFunc = create_function('&$obj, $data', $this->core->handlers[$alert]['code']);
                        $newFunc($this, $data);
                    }
                }
                // save the messages
                $_tmp = array();
                if (in_array($data['message_alert'], $this->core->ignorecmd)) $data['message_text'] = '';
                foreach ($data as $k => $v) $_tmp[$k] = '"'.$v.'"';
                Db::execute('INSERT INTO atac_messages('.join(',',array_keys($_tmp)).') VALUES('.join(',', array_values($_tmp)).')');
            } else {
                throw new ATACException('Data stream to ' . __CLASS__ . '::' . __FUNCTION__ . ' is invalid');
            }
        }

        public function ignoreAlert($alert)
        {
            if (!in_array($alert, $this->core->ignorecmd)) $this->core->ignorecmd[] = $alert;
        }

        /**
         * aim_parse_command: Parse commands via ATAC::SFLAP::aim_sflap_recv
         *
         * @param string $cmd Command issued
         * @param string $args List of arguments associated with command
         * @access public
         * @return void
         */
         public function aim_parse_command($cmd, $args)
         {
             $cmd = strtoupper($cmd);
             $cbf = $this->core->user['callbacks'] . $cmd . '.php';
             $files = get_included_files();

             foreach ($files as $file) {
                 if (basename($file) == basename($cbf)) {
                     if (false === call_user_func_array(array($cmd, 'parse'), array($args, &$this))) {
                         throw new ATACException('Parse method for ' . $cmd . ' failed.  Function call failed.');
                     }
                     break;
                 } else {
                    //throw new ATACException('Parse method for ' . $cmd . ' failed.  Callback file not found.');
                 }
             }
         }

}

?>